from Test import AbstractTest
from MySocket import User, Event
'''
1. Creates 4 messages and sends them out of order
2. One of the messages is wrong: it's related to a user that is not registered
3. Messages should be received in the correct order and wrong message should be skipped
'''
class TestMessageOrder(AbstractTest):
    def __init__(self):
        test_name = "Message Order Test"
        super(TestMessageOrder, self).__init__(test_name)

    def run(self):
        # message to be sent
        message4 = "4|B\n"
        # extra message that should be skipped
        # as there is no user wit proper id
        message3 = "3|P|1|2\n"
        message2 = "2|B\n"
        message1 = "1|B\n"

        # connect all the users
        user_1 = User()
        user_1.connect()
        user_1.mysend("1\n")
        # create event
        event = Event()
        event.connect()
        event.mysend(message4)
        event.mysend(message3)
        event.mysend(message2)
        event.mysend(message1)
        # validation
        rev1 = user_1.myreceive(len(message1))
        rev2 = user_1.myreceive(len(message2))
        rev3 = user_1.myreceive(len(message4))

        if(rev1 != message1):
            return False;
        if(rev2 != message2):
            return False;
        if(rev3 != message4):
            return False;

        return True